import instance from "../../utils/util"
import {
  ComponentWithStore
} from 'mobx-miniprogram-bindings'
// pages/cart/cart.js
const computedBehavior = require("miniprogram-computed").behavior;
const app = getApp()
Page({
  //注册计算属性
  behaviors: [computedBehavior],
  computed: {
    selectAllStatus(data) {

      return data.cart.every((item) => item.isChecked === 1)

    },
    totalPrice(data) {
      console.log(data)
      let num = 0;
      data.cart.forEach(el => {
        if (el.isChecked == 1) {
          num += (el.price * el.count)

        }
      })
      console.log(num)
      return num;
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    cart: [],
    show: false,
    qx: true,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  onShow() {
    if (wx.getStorageSync('token')) {

      this.getiinfo()
    } else {
      this.setData({
        show: false
      })
    }
  },
  login() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
   shanchu(e) {
    wx.showModal({
      title: '删除',
      content: '是否确认删除该商品？',
         complete: async(res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          const res1 = await instance.get(`/cart/delete/${ e.currentTarget.dataset.id}`)
          if (res1.code == 200) {
            this.getiinfo()
          }
        }
      }
    })
  
  },
  onSubmit(){
    console.log(1213)
    if(this.data.totalPrice === 0){
      wx.showToast({
        title: '请选择商品',
        icon:"none"
      })
      return
    }else{
      wx.navigateTo({
        url: '/pages/jsdd/jsdd',
      })
    }

  },
  onChange1(e) {
    console.log(e)
    const {
      detail
    } = e
    const {
      id,
      index
    } = e.currentTarget.dataset
    this.setData({
      [`cart[${index}].count`]: detail
    })
  },
  async onchange(e) {
    console.log(e)
    const {
      detail
    } = e
    const {
      id,
      index
    } = e.currentTarget.dataset
    const ischecked = detail ? 1 : 0
    const res = await instance.get(`/cart/checkCart/${id}/${ischecked}`);
    console.log(res)
    if (res.code == 200) {
      this.setData({
        [`cart[${index}].isChecked`]: ischecked
      })
    }
    // this.getiinfo()
  },
  async getiinfo() {
    const res = await instance.get("/cart/getCartList");
    console.log(res)
    this.setData({
      show: true,
      cart: res.data
    })
    this.data.cart.forEach(el => {
      el.checked = true
    })
    this.setData({
      cart: this.data.cart
    })
  },

  async selectall(e) {
    console.log(e)
    const ischecked = e.detail ? 1 : 0;
    const res = await instance.get(`/cart/checkAllCart/${ischecked}`)
    console.log(res)

    if (res.code == 200) {
      const newcart = this.data.cart;
      newcart.forEach(el => {
        el.isChecked = ischecked;
      })
      this.setData({
        cart: newcart
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }



  /**
   * 生命周期函数--监听页面显示
   */

})